package Datebase;

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class tinformation extends JFrame {
    public static void main(String[] args) {
        System.setProperty("sun.java2d.noddraw", "true");
        new tinformation();
    }

    public tinformation(){
        JFrame mod = new JFrame("学生信息录入系统");
        URL resource = tinformation.class.getClassLoader().getResource("kun.png");
        Image image = new ImageIcon(resource).getImage();
        mod.setIconImage(image);
        mod.setLayout(null);
        mod.setSize(800,600);
        mod.setLocationRelativeTo(null);

        JLabel tot = new JLabel("请输入要录入的信息:");
        tot.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        tot.setBounds(0,0,350,50);
        mod.add(tot);
        mod.setVisible(true);



        JLabel label2 = new JLabel("学号:");
        JTextField textField2 = new JTextField();
        textField2.setColumns(10);
        label2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label2.setBounds(300,60,150,60);
        textField2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        textField2.setBounds(360,80,120,25);
        mod.add(label2);
        mod.add(textField2);



        JLabel label3 = new JLabel("密码:");
        JTextField textField3 = new JTextField();
        textField3.setColumns(10);
        label3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label3.setBounds(300,120,150,60);
        textField3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        textField3.setBounds(360,140,120,25);
        mod.add(label3);
        mod.add(textField3);

        JLabel label4 = new JLabel("姓名:");
        JTextField textField4 = new JTextField();
        textField4.setColumns(10);
        label4.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label4.setBounds(300,180,150,60);
        textField4.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        textField4.setBounds(360,200,120,25);
        mod.add(label4);
        mod.add(textField4);

        JLabel label5 = new JLabel("性别:");
        JTextField textField5 = new JTextField();
        textField5.setColumns(10);
        label5.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label5.setBounds(300,240,150,60);
        textField5.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        textField5.setBounds(360,260,120,25);
        mod.add(label5);
        mod.add(textField5);

        JLabel label6 = new JLabel("院系:");
        JTextField textField6 = new JTextField();
        textField6.setColumns(10);
        label6.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label6.setBounds(300,300,150,60);
        textField6.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        textField6.setBounds(360,320,120,25);
        mod.add(label6);
        mod.add(textField6);

        JLabel label7 = new JLabel("专业:");
        JTextField textField7 = new JTextField();
        textField7.setColumns(10);
        label7.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label7.setBounds(300,360,150,60);
        textField7.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        textField7.setBounds(360,380,120,30);
        mod.add(label7);
        mod.add(textField7);

        JLabel label8 = new JLabel("班级:");
        JTextField textField8 = new JTextField();
        textField8.setColumns(10);
        label8.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label8.setBounds(300,420,150,60);
        textField8.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        textField8.setBounds(360,440,120,30);
        mod.add(label8);
        mod.add(textField8);


        JButton button= new JButton("确认录入");
        button.setFont(new Font("微软雅黑", Font.PLAIN, 18));

        button.setBounds(600,475,150,60);
        mod.add(button);
        mod.setVisible(true);

        mod.repaint();

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                try {
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
                    String user = "HR";
                    String password = "root";
                    Connection conn = DriverManager.getConnection(dbURL, user, password);
                    String sql = "INSERT INTO STUDENT values ('" + textField2.getText()+ "','" + textField3.getText() + "','" + textField4.getText() + "','" + textField5.getText() + "','"+ textField6.getText()+"','"+ textField7.getText()+"','" +textField8.getText()+"')";


                    PreparedStatement pstm = conn.prepareStatement(sql);
                    ResultSet rs = pstm.executeQuery();

                    JOptionPane.showMessageDialog(null, "录入成功！","录入状态", JOptionPane.INFORMATION_MESSAGE);
                } catch (ClassNotFoundException cnfe) {
                    cnfe.printStackTrace();
                    JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (SQLException sqle) {
                    sqle.printStackTrace();
                    JOptionPane.showMessageDialog(null, "学号为空", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });


    }
}
